package inet.app.model.cau;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;

public class KetQua  implements Serializable{
	ArrayList<Giai>giaiList;//0,1,2..,7
	String calendar;
	Calendar calendar2;
	ArrayList<CauPosition> cauPosList;
	
	public KetQua(ArrayList<Giai>giaiList,ArrayList<CauPosition> cauPosList, String calendar){
		this.giaiList = giaiList;
		this.calendar = calendar;
		this.cauPosList = cauPosList;
	}
	
	public KetQua(ArrayList<Giai>giaiList, String ngayTuongThuat){
		this.giaiList = giaiList;
		this.calendar = ngayTuongThuat;
	}
	public Calendar getCalendar2() {
		return calendar2;
	}
	
	public String getCalendar() {
		return calendar;
	}


	public void setCalendar(String calendar) {
		this.calendar = calendar;
	}

	public ArrayList<Giai> getGiaiList() {
		return giaiList;
	}

	public void setGiaiList(ArrayList<Giai> giaiList) {
		this.giaiList = giaiList;
	}

	public ArrayList<CauPosition> getCauPosList() {
		return cauPosList;
	}

	public void setCauPosList(ArrayList<CauPosition> cauPosList) {
		this.cauPosList = cauPosList;
	}

	public boolean hasCau(int giaiNumber, int resultNumber){		
		if(cauPosList==null || cauPosList.size()==0)
			return false;
		for(CauPosition pos: cauPosList){
			if(pos.giaiIndex==giaiNumber && pos.resultIndex==resultNumber)
				return true;
		}
		
		return false;
	}
	
	public ArrayList<CauPosition> getCau(int giaiNumber, int resultNumber){
		if(cauPosList==null || cauPosList.size()==0)
			return null;
		ArrayList<CauPosition>cauList = new ArrayList<CauPosition>();
			
		for(CauPosition pos: cauPosList){
			if(pos.giaiIndex==giaiNumber && pos.resultIndex==resultNumber)
				cauList.add(pos);
		}
		
		return cauList;
	}	

	public int getValueAtPos(int pos){
		if(Global.mien==2)
			return getValueAtPosMT(pos);
		else if(Global.mien==3){
			return getValueAtPosMN6so(pos);
		}
			
		int gIndex = 0;
		int posInGiai = 0;
		
		if(pos<5){
			//giai DB
			gIndex = 0;
			posInGiai = pos;
			
		}else if(pos<10){
			gIndex = 1;
			posInGiai = pos-5;
		}			
		else if(pos<20){
			gIndex = 2;
			posInGiai = pos-10;
		}			
		else if(pos<50){
			gIndex = 3;
			posInGiai = pos-20;
		}			
		else if(pos<66){
			gIndex = 4;
			posInGiai = pos-50;
		}			
		else if(pos<90){
			gIndex = 5;
			posInGiai = pos-66;
		}			
		else if(pos<99){
			gIndex = 6;
			posInGiai = pos-90;
		}			
		else{
			gIndex = 7;
			posInGiai = pos-99;
		}
			
		Giai currentGiai = giaiList.get(gIndex);
		String allStr = "";
		for(String s: currentGiai.getResultList()){			
			allStr+=s;
		}
		String tmp = new String(allStr);
		String s = tmp.substring(posInGiai, posInGiai+1);
		return Integer.parseInt(s);
	}
	
	public int getValueAtPosMT(int pos){
		int gIndex = 0;
		int posInGiai = 0;
		
		if(pos<5){
			//giai DB
			gIndex = 0;
			posInGiai = pos;
			
		}else if(pos<10){
			gIndex = 1;
			posInGiai = pos-5;
		}			
		else if(pos<15){
			gIndex = 2;
			posInGiai = pos-10;
		}			
		else if(pos<25){
			gIndex = 3;
			posInGiai = pos-15;
		}			
		else if(pos<60){
			gIndex = 4;
			posInGiai = pos-25;
		}			
		else if(pos<64){
			gIndex = 5;
			posInGiai = pos-60;
		}			
		else if(pos<76){
			gIndex = 6;
			posInGiai = pos-64;
		}			
		else if(pos<79){
			gIndex = 7;
			posInGiai = pos-76;
		}else{
			gIndex = 8;
			posInGiai = pos-79;
		}
			
		
		Giai currentGiai = giaiList.get(gIndex);
		String allStr = "";
		for(String s: currentGiai.getResultList()){			
			allStr+=s;
		}
		String tmp = new String(allStr);
		String s = tmp.substring(posInGiai, posInGiai+1);
		return Integer.parseInt(s);
	}
	
	public int getValueAtPosMN6so(int pos){
		int gIndex = 0;
		int posInGiai = 0;
		
		if(pos<6){
			//giai DB
			gIndex = 0;
			posInGiai = pos;
			
		}else if(pos<11){
			gIndex = 1;
			posInGiai = pos-6;
		}			
		else if(pos<16){
			gIndex = 2;
			posInGiai = pos-11;
		}			
		else if(pos<26){
			gIndex = 3;
			posInGiai = pos-16;
		}			
		else if(pos<61){
			gIndex = 4;
			posInGiai = pos-26;
		}			
		else if(pos<65){
			gIndex = 5;
			posInGiai = pos-61;
		}			
		else if(pos<77){
			gIndex = 6;
			posInGiai = pos-65;
		}			
		else if(pos<80){
			gIndex = 7;
			posInGiai = pos-77;
		}else{
			gIndex = 8;
			posInGiai = pos-80;
		}
			
		Giai currentGiai = giaiList.get(gIndex);
		String allStr = "";
		for(String s: currentGiai.getResultList()){			
			allStr+=s;
		}
		
		/*Giai currentGiai = giaiList.get(gIndex);
		String allStr;
		if(currentGiai.getResultList().get(0).toCharArray().length==6){
			allStr = currentGiai.getResultList().get(0).substring(1);
			System.out.println("allStr after cut:"  + allStr);
		}		 
		else allStr = currentGiai.getResultList().get(0);
		
		int size = currentGiai.getResultList().size();
		for(int i = 1; i < size; i ++){
			allStr+= currentGiai.getResultList().get(i);
		}*/
		/*for(String s: currentGiai.getResultList()){			
			allStr+=s;
		}*/
		System.out.println("pos=" +pos + ". pos in giai:" + posInGiai);
		System.out.println("giai:"  + allStr);
		String tmp = new String(allStr);
		String s = tmp.substring(posInGiai, posInGiai+1);
		return Integer.parseInt(s);
	}
}	
